<?php
/**
 * This file contains several general functions that
 * can be used throughout the framework to accomplish
 * specific tasks.
 *
 * @author Nathan Rice
 * @package Prodigy
 **/

/**
 * Because the WordPress function <code>comments_number</code> will only
 * <code>ECHO</code> the output, this function uses an output buffer to
 * <code>RETURN</code> the output.
 *
 * Use this function instead of <code>comments_number</code>
 *
 * @since 0.1
 * @uses ob_start, ob_get_clean, ob_end_clean
 * @uses comments_number
 */
function prodigy_comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
	ob_start(); comments_number($zero, $one, $more);
	$output = ob_get_clean(); ob_end_clean();
	
	return apply_filters('prodigy_comments_number', $output, $number);
}

/**
 * Because the WordPress function <code>comments_popup_link</code> will only
 * <code>ECHO</code> the output, and doesn't work on single posts and pages,
 * this function uses an output buffer to <code>RETURN</code> the output.
 *
 * @uses ob_start, ob_get_clean, ob_end_clean
 * @uses prodigy_comments_number
 *
 * @since 0.1
 */
// This function RETURNS the number of comments the post currently has, wrapped in anchor tags
function prodigy_comments_number_link( $zero = false, $one = false, $more = false, $deprecated = '') {
	$output = '<a href="'.get_permalink().'#comments">'.prodigy_comments_number($zero, $one, $more).'</a>';
	return apply_filters('prodigy_comments_number_link', $output);
}

/**
 * Because the WordPress function <code>edit_post_link</code> will only
 * <code>ECHO</code> the output, this function uses an output buffer to
 * <code>RETURN</code> the output.
 *
 * @uses ob_start, ob_get_clean, ob_end_clean
 * @uses edit_post_link
 *
 * @since 0.1
 */
function prodigy_edit_post_link( $link = 'Edit This', $before = '', $after = '' ) {
	ob_start(); edit_post_link($link, $before, $after);
	$output = ob_get_clean(); ob_end_clean();
	
	return apply_filters('prodigy_edit_post_link', $output);
}

/**
 * This function can be used to easily and efficiently pull data from a
 * post/page custom field. Returns FALSE if field is blank or not set.
 *
 * @param $field used to indicate the custom field key
 * @param $echo used to determine of the function echos or returns the output
 *
 * @since 0.1
 */
function get_custom_field($field, $echo = TRUE) {
	global $post;
	$custom_field = stripslashes(get_post_meta($post->ID, $field, true));
	if($custom_field == FALSE) return FALSE;
	if($echo) { echo $custom_field; } else { return $custom_field; }
}

/**
 * This function is used to pull theme option values.
 * It will parse the options in the DB with those of a
 * user-filtered option array, so that theme authors can
 * set defaults, and use options without an options page.
 *
 * **NOTE**:
 * Prodigy options should be stored with autoload ON
 * so that the value of get_option('prodigy_options')
 * will be cached on each pageload. Multiple calls using
 * prodigy_option will then only query the DB once, and
 * will not affect DB performance any more than a
 * global variable array would.
 *
 * @param $opt the key for the option you would like to pull
 * @param $field the option array in the DB you would like to query
 *
 * @since 0.1
 */
function prodigy_option($opt, $field = 'Prodigy-options') {
	$prodigy_options_db = get_option($field); // pull from the db
	$prodigy_options_array = apply_filters($field, array()); // pull from user array (by filter)
	
	$prodigy_options = wp_parse_args($prodigy_options_db, $prodigy_options_array); // merge db and array, giving db priority
	
	if($prodigy_options[$opt] && !empty($prodigy_options[$opt])) return $prodigy_options[$opt];
	else return FALSE;
}
?>